package com.justech.eat.service.component.impl;

import com.justech.eat.entity.AccMonitorLog;
import com.justech.eat.entity.User;
import com.justech.eat.service.component.ProcessingJobNoAccMonitorLogParser;
import com.justech.eat.service.impl.AccMonitorLogParserImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: eat-service
 * @description:
 * @author: feifei.li
 * @create: 2020-06-08 14:47
 **/
public class ProcessingJobNoAccMonitorLogParserImpl extends AccMonitorLogParserImpl implements ProcessingJobNoAccMonitorLogParser {

    private List<User> users;

    private Map<Integer, User> sysUserMap;

    public ProcessingJobNoAccMonitorLogParserImpl(String date) {
        super(date);
    }

    private void usersToMap() {
        this.sysUserMap = new HashMap<>(users.size());
        for (User user : this.users) {
            if (user.getPin() == null) continue;
            try {
                Integer sourceNo = Integer.valueOf(user.getPin());
                sysUserMap.put(sourceNo, user);
            } catch (Exception e) {
                continue;
            }
        }
    }

    private User get(String pin) {
        Integer pinInt = null;
        try {
            pinInt = Integer.valueOf(pin);
        } catch (Exception e) {
            return null;
        }
        return this.sysUserMap.get(pinInt);
    }

    /**
     * 设置用户信息集合
     *
     * @param users
     */
    @Override
    public void setSysUsers(List<User> users) {
        this.users = users;
        this.usersToMap();
    }

    /**
     * 主要目的是提取出数据中的餐别
     *
     * @param log
     * @return
     */
    @Override
    public AccMonitorLog parse(AccMonitorLog log) {
        AccMonitorLog parse = super.parse(log);
        User sysUser = get(parse.getPin());
        if (sysUser != null) {
            parse.setJobNumber(sysUser.getJobNumber());
        }
        return parse;
    }
}
